package com.cssl.controller;

import com.cssl.pojo.Users;
import com.cssl.server.UsersService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@SuppressWarnings("all")
@RestController
public class UsersController {
    @Autowired
    private UsersService service;

    @RequestMapping("/map")
    public Map<String,String> findMap(@RequestParam Map<String,String> map){
        System.out.println("provier map:"+map);
        return map;
    }

    /**
     * @RequestBody必须有
     * @param user
     * @return
     */
    @HystrixCommand(fallbackMethod = "fallback")
    @PostMapping("/findUsers")
    public Users findUsers(@RequestBody Users user){
        System.out.println("provider findUsers:"+user.getId()+"\tusername:"+user.getUsername());
        return service.getUsersById(user);
    }

    public Users fallback(@RequestBody Users user){
        System.out.println("fallback");
        user.setId(250);
        user.setUsername("用户不存在");
        user.setPassword("222");
        return user;
    }

    @PostMapping("/login")
    public String login(String username,String password){
        System.out.println("provider login:"+username);
        if(service.findByUsername(username,password)){
            return "success";
        }
        return "input";
    }
}
